//
//  MeViewController.m
//  SinaWebo
//
//  Created by 亦心 on 15/3/7.
//  Copyright (c) 2015年 亦心. All rights reserved.
//

#import "MeViewController.h"
#import "SettingArrowItem.h"
#import "UIBarButtonItem+Extension.h"
#import "UserCenterViewController.h"
#import "MeDetailViewController.h"
#import "WebViewController.h"
#import "UserPerfenceUtil.h"
#import "SettingCardCell.h"
#import "SettingGroup.h"
#import "MeHeadView.h"
#import "MeService.h"
#import "CardGroup.h"
#import "Card.h"

#define SettingFilePath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"Setting.data"]

@interface MeViewController ()<MeHeadViewDelegate>
@property (strong, nonatomic) NSArray *dataArray;
@end


@implementation MeViewController
static NSString *const CELL_ID = @"SettingCardCell";
static NSString *const HEAD_ID = @"headerView";

- (NSArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}

- (instancetype)init{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataArray = [NSKeyedUnarchiver unarchiveObjectWithFile:SettingFilePath];
    if (self.dataArray.count > 0) {
        [self.tableView reloadData];
    }
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTitle:@"设置" target:self action:nil];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MeHeadView class]) bundle:nil] forHeaderFooterViewReuseIdentifier:HEAD_ID];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SettingCardCell class]) bundle:nil] forCellReuseIdentifier:CELL_ID];
    self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 5, 0);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self getSettingData];
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 165;
    }
    return 5;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    Card *card = _dataArray[section];
    return card.card_group.count ;
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        MeHeadView *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HEAD_ID];
        headView.delegate = self;
        return headView;
    }
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SettingCardCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_ID forIndexPath:indexPath];
    Card *card = _dataArray[indexPath.section];
    cell.model = card.card_group[indexPath.row];
    return cell;
}

#pragma mark HeadViewDelegate
- (void)onClickHeadImg:(MeHeadView *)headView{
    MeDetailViewController *meDetailVC = [[MeDetailViewController alloc] init];
    [self.navigationController pushViewController:meDetailVC animated:YES];
}

- (void)onClickVip:(MeHeadView *)headView{
    UserCenterViewController *userCenterVC = [[UserCenterViewController alloc] init];
    [self.navigationController pushViewController:userCenterVC animated:YES];
}

- (void)getSettingData{
    MeSettingRequest *request = [[MeSettingRequest alloc] init];
    request.gsid = @"_2A256dYiwDeTxGeNL7lMX-C3FyjyIHXVXIpt4rDV6PUJbkdANLXjVkWqL6tBZdTePbgYfEFLycZxkT9rtyA..";
    request.b = @"1";
    request.from = @"1066093010";
    request.c = @"iphone";
    request.v_p = @"31";
    request.skin = @"default";
    request.s = @"392e5974";
    request.uid = [UserPerfenceUtil account].uid;;
    [MeService getMeSettingWithRequest:request success:^(MeSettingResponse *response) {
        if (response.cards) {
            [NSKeyedArchiver archiveRootObject:response.cards toFile:SettingFilePath];
            self.dataArray = response.cards;
            [self.tableView reloadData];
        }
        
    } error:nil];
}

@end
